﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using YBApproval.Model;
using YBApproval.Common;
using YBApproval.IDAL;
using YBApproval.BLL;
using System.Collections.Generic;

namespace YBApproval
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Session.Abandon();
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            UserProfile inUser = new UserProfile
            {
                LoginName = txtUsername.Text, //GetUserName();
                Pwd = Encrypt.EncryptDES(txtPassword.Text, Encrypt.DefaultEncryptKey)
            };
            
            //string domainName = GetDomainName(txtUsername.Text);

            try
            {
                UserProfile cUser;
                using (IUserProfile _userProfileDAL = DALFactory.GetDAL<IUserProfile>())
                {
                    cUser = _userProfileDAL.UserLogin(inUser);
                }
                if (cUser != null)
                {
                    YBPSession.Instance.CurrentUser = cUser;
                    using (IUserProfile _userProfileDAL = DALFactory.GetDAL<IUserProfile>())
                    {
                        if (cUser.Delegate.HasValue)
                        {
                            YBPSession.Instance.CurUserDelegate = _userProfileDAL.GetUserByID(cUser.Delegate.Value);
                        }                        
                    }
                    using (IRole _RoleDAL = DALFactory.GetDAL<IRole>())
                    {
                        if (!string.IsNullOrEmpty(cUser.RoleId))
                        {
                            var strIds = cUser.RoleId.Split(',').ToList<string>();
                            List<int> Ids = new List<int>();
                            foreach (var item in strIds)
                            {
                                Ids.Add(int.Parse(item));
                            }
                            YBPSession.Instance.MyRoleList = _RoleDAL.GetUserRoleList(Ids);
                        }
                        YBPSession.Instance.AllRolesList = _RoleDAL.GetAll();
                    }

                    //ScriptManager.RegisterStartupScript(Page, GetType(), "", "document.referrer", true);

                    Response.Redirect(@"~/Default.aspx");
                }
                else
                {
                    errorLabel.Text = "登陆失败，请检查用户名密码是否正确！";
                }
            }
            catch (Exception ex)
            {
                errorLabel.Text = ex.Message;
            }            
                
        }
    }
}